package handler

import (
	"net/http"

	"project-info/src/lib")

// TestPage 测试页面，用于演示OIDC流程
func TestPage(c *lib.GinContext) error {
	testHTML := `
<!DOCTYPE html>
<html>
<head>
    <title>OIDC 测试页面</title>
    <meta charset="UTF-8">
    <style>
        body { 
            font-family: Arial, sans-serif; 
            max-width: 800px; 
            margin: 50px auto; 
            padding: 20px; 
            line-height: 1.6;
        }
        .section {
            background-color: #f8f9fa;
            padding: 20px;
            border-radius: 8px;
            margin-bottom: 20px;
            border-left: 4px solid #007bff;
        }
        .endpoint {
            background-color: #e9ecef;
            padding: 10px;
            border-radius: 4px;
            margin: 10px 0;
            font-family: monospace;
        }
        .button {
            display: inline-block;
            padding: 10px 20px;
            background-color: #007bff;
            color: white;
            text-decoration: none;
            border-radius: 4px;
            margin: 5px;
        }
        .button:hover {
            background-color: #0056b3;
        }
        .client-info {
            background-color: #fff3cd;
            border: 1px solid #ffeaa7;
            padding: 15px;
            border-radius: 4px;
            margin: 10px 0;
        }
        .code-block {
            background-color: #f8f9fa;
            border: 1px solid #e9ecef;
            padding: 15px;
            border-radius: 4px;
            font-family: monospace;
            font-size: 14px;
            overflow-x: auto;
        }
        h1 { color: #333; }
        h2 { color: #666; }
        h3 { color: #888; }
    </style>
</head>
<body>
    <h1>🔐 OIDC 服务器测试页面</h1>
    
    <div class="section">
        <h2>📋 服务器信息</h2>
        <p><strong>服务器地址:</strong> http://localhost:8080</p>
        <p><strong>OIDC Issuer:</strong> http://localhost:8080</p>
        <p><strong>状态:</strong> <span style="color: green;">✅ 运行中</span></p>
    </div>

    <div class="section">
        <h2>🔍 发现端点测试</h2>
        <p>测试OIDC服务器的发现端点，获取服务器配置信息：</p>
        <div class="endpoint">GET /oidc/.well-known/openid_configuration</div>
        <a href="/oidc/.well-known/openid_configuration" class="button" target="_blank">测试发现端点</a>
        <a href="/oidc/.well-known/jwks.json" class="button" target="_blank">测试JWKS端点</a>
    </div>

    <div class="section">
        <h2>🔑 预配置客户端</h2>
        <div class="client-info">
            <h3>测试客户端 1</h3>
            <p><strong>Client ID:</strong> test_client</p>
            <p><strong>Client Secret:</strong> test_secret</p>
            <p><strong>重定向URI:</strong> http://localhost:3000/callback, https://oauth.pstmn.io/v1/callback</p>
            <p><strong>支持的Scope:</strong> openid, profile, roles, permissions</p>
        </div>
        <div class="client-info">
            <h3>演示应用</h3>
            <p><strong>Client ID:</strong> demo_app</p>
            <p><strong>Client Secret:</strong> demo_secret</p>
            <p><strong>重定向URI:</strong> http://localhost:8080/auth/callback</p>
            <p><strong>支持的Scope:</strong> openid, profile</p>
        </div>
    </div>

    <div class="section">
        <h2>👤 测试用户账户</h2>
        <div class="client-info">
            <p><strong>管理员账户:</strong> admin / admin123</p>
            <p><strong>普通用户:</strong> user / user123</p>
        </div>
    </div>

    <div class="section">
        <h2>🚀 快速测试链接</h2>
        <p>点击以下链接快速测试OIDC授权流程：</p>
        
        <h3>基础授权流程测试</h3>
        <a href="/oidc/auth?response_type=code&client_id=test_client&redirect_uri=https://oauth.pstmn.io/v1/callback&scope=openid+profile&state=test_state_123" class="button">测试授权流程 (Postman回调)</a>
        
        <h3>包含角色权限的授权测试</h3>
        <a href="/oidc/auth?response_type=code&client_id=test_client&redirect_uri=https://oauth.pstmn.io/v1/callback&scope=openid+profile+roles+permissions&state=test_state_456" class="button">测试完整权限授权</a>
        
        <h3>PKCE流程测试</h3>
        <a href="/oidc/auth?response_type=code&client_id=test_client&redirect_uri=https://oauth.pstmn.io/v1/callback&scope=openid+profile&state=pkce_test&code_challenge=E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM&code_challenge_method=S256" class="button">测试PKCE授权</a>
    </div>

    <div class="section">
        <h2>📖 API端点列表</h2>
        <h3>OIDC协议端点</h3>
        <div class="endpoint">GET /oidc/.well-known/openid_configuration - 发现端点</div>
        <div class="endpoint">GET /oidc/.well-known/jwks.json - JWKS端点</div>
        <div class="endpoint">GET/POST /oidc/auth - 授权端点</div>
        <div class="endpoint">POST /oidc/token - 令牌端点</div>
        <div class="endpoint">GET/POST /oidc/userinfo - 用户信息端点</div>
        <div class="endpoint">POST /oidc/revoke - 令牌撤销端点</div>
        <div class="endpoint">POST /oidc/introspect - 令牌内省端点</div>
        <div class="endpoint">GET/POST /oidc/logout - 登出端点</div>
        
        <h3>调试端点</h3>
        <div class="endpoint">GET /oidc/health - 健康检查</div>
        <div class="endpoint">GET /oidc/debug/tokens - 令牌统计信息</div>
        <div class="endpoint">POST /oidc/debug/cleanup - 清理测试数据</div>
    </div>

    <div class="section">
        <h2>🔧 Postman测试配置</h2>
        <p>使用Postman测试OIDC流程的配置示例：</p>
        
        <h3>1. 获取授权码</h3>
        <div class="code-block">
GET /oidc/auth?response_type=code&client_id=test_client&redirect_uri=https://oauth.pstmn.io/v1/callback&scope=openid+profile&state=random_state
        </div>
        
        <h3>2. 交换访问令牌</h3>
        <div class="code-block">
POST /oidc/token
Content-Type: application/x-www-form-urlencoded

grant_type=authorization_code&code={从步骤1获取的code}&redirect_uri=https://oauth.pstmn.io/v1/callback&client_id=test_client&client_secret=test_secret
        </div>
        
        <h3>3. 获取用户信息</h3>
        <div class="code-block">
GET /oidc/userinfo
Authorization: Bearer {从步骤2获取的access_token}
        </div>
    </div>

    <div class="section">
        <h2>🐛 调试工具</h2>
        <a href="/oidc/health" class="button" target="_blank">健康检查</a>
        <a href="/oidc/debug/tokens" class="button" target="_blank">查看令牌统计</a>
        <button onclick="cleanupData()" class="button" style="background-color: #dc3545;">清理测试数据</button>
    </div>

    <script>
        function cleanupData() {
            if (confirm('确定要清理所有测试数据吗？这将删除所有令牌、授权码和会话。')) {
                fetch('/oidc/debug/cleanup', {
                    method: 'POST'
                })
                .then(response => response.json())
                .then(data => {
                    alert('清理完成: ' + data.message);
                })
                .catch(error => {
                    alert('清理失败: ' + error);
                });
            }
        }
    </script>
</body>
</html>
`

	c.Response().Header().Set("Content-Type", "text/html; charset=utf-8")
	c.HTML(http.StatusOK, testHTML, nil)
	return nil
}